package tt.dz.mvc.controller.base;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsFactoryList;
import tt.dz.entity.exception.OmsServiceException;
import tt.dz.service.base.FactoryService;
import tt.dz.util.ActionUtils;
import tt.dz.util.OmsConstant.MsgCodeEnum;

@Controller
@RequestMapping("/factotyApi")
public class FactoryListAtion extends BaseAction
{
	@Autowired
	private FactoryService factoryServiceImpl;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="pageFactorys",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<OmsFactoryList> queryPageFactoryList(HttpServletRequest request, HttpServletResponse response)
	{
		OmsCommonParam param = this.buildQueryParam(request, true);
		try 
		{
			return factoryServiceImpl.findPageFactoryList(param,this.getObjFromRequest(request,false,true));
		} 
		catch (OmsServiceException e) 
		{
			return (JsonForList<OmsFactoryList>) this.buildOtherJsonForList(
					MsgCodeEnum.ERROR400.getMsgCode(), e.getMessage());
		}
	}
	
	@RequestMapping(value="addFactory",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> addFactory(HttpServletRequest request, HttpServletResponse response)
	{
		String repeatPassword = request.getParameter("repeatPassword"); 
		OmsFactoryList factory = this.requestParam2Obj(request, true);
		if (!factory.getFac_password().endsWith(repeatPassword)) {
			JsonCommon jfl = new JsonCommon<>();
			jfl.setCode(MsgCodeEnum.ERROR400.getMsgCode());
			jfl.setMsg("two times the password is not consistent");
			return jfl;
		}
		return factoryServiceImpl.addFactoryList(factory);
	}
	
	@RequestMapping(value="updateFactory/{factory_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> updateFactory(@PathVariable(value="factory_id") String factory_id,HttpServletRequest request, HttpServletResponse response)
	{
		String repeatPassword = request.getParameter("repeatPassword"); 
		OmsFactoryList factory = this.requestParam2Obj(request, false);
		factory.setFactory_id(factory_id);
		if (!factory.getFac_password().endsWith(repeatPassword)) {
			JsonCommon jfl = new JsonCommon<>();
			jfl.setCode(MsgCodeEnum.ERROR400.getMsgCode());
			jfl.setMsg("two times the password is not consistent");
			return jfl;
		}
		return factoryServiceImpl.updateFactoryList(factory);
	}
	
	@RequestMapping(value="deleteFactory/{factory_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> deleteFactory(@PathVariable(value="factory_id") String factory_id,HttpServletRequest request, HttpServletResponse response)
	{
		return factoryServiceImpl.deleteFactoryById(factory_id);
	}
		
	private OmsFactoryList requestParam2Obj(HttpServletRequest request,boolean isCreate)
	{
		OmsFactoryList ofl = new OmsFactoryList();
		String oper_merchant_id = request.getParameter("oper_merchant_id");
		ofl.setOper_merchant_id(oper_merchant_id);
		String brand = request.getParameter("brand");       
		ofl.setBrand(brand);
		String factory_name = request.getParameter("factory_name");   
		ofl.setFactory_name(factory_name);
		String address = request.getParameter("address");   
		ofl.setAddress(address);
//		String fac_login_name = request.getParameter("fac_login_name");  
//		ofl.setFac_login_name(fac_login_name);
		String fac_password = request.getParameter("fac_password");    
		ofl.setFac_password(fac_password);
		String status = request.getParameter("status");    
		String valid_flag = request.getParameter("valid_flag");
		
		if (StringUtils.isNumeric(status))
		{
			ofl.setStatus(Integer.parseInt(status));
		}
		String loginUserId = this.getLoginUserId(request);
		Date date = new Date();
		if (isCreate)
		{
			ofl.setCreate_oper_id(loginUserId);		
			ofl.setCreate_time(date);
			ofl.setStatus(1);
		}
		ofl.setValid_flag(Integer.valueOf(valid_flag));
		ofl.setUpdate_oper_id(loginUserId);
		ofl.setUpdate_time(date);
		
		return ofl;
	}
	
	private OmsFactoryList getObjFromRequest(HttpServletRequest request,boolean isCreate,boolean isFind)
	{
		String loginUserId = this.getLoginUserId(request);
		return ActionUtils.getParamInstance(OmsFactoryList.class, ActionUtils.getParameterMap(request),loginUserId,isCreate,isFind);
	}
}